package com.baking.controller;

import com.baking.model.Menu;
import com.baking.model.User;
import com.baking.service.ILoginService;
import com.baking.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author:wrb
 * @Date :2019/12/21/20:04
 * @Describtion:
 **/
@Controller
public class loginController {

    @Autowired
    ILoginService iLoginService;
    @RequestMapping("login")
    private String login(){
        return "/login";
    }

    /** @Description:点击登录
     * @Author:wrb
     * @Date:2019/12/22/15:38
     * @Param 
     * @return java.lang.String
     */
    @RequestMapping("submit")
    @ResponseBody
    private Msg submit(HttpServletRequest request, HttpServletResponse response, User user){
        HttpSession session = request.getSession();
        List<User> userList = iLoginService.getUser(user);
        Msg msg = new Msg();
        if (userList!=null && userList.size()>0){
            msg.deSuccess(userList);
            session.setAttribute("user",userList.get(0));
        }else {
            msg.doError("用户名或者密码错误");
        }
        return msg;
    }

    @RequestMapping("/home")
    private String home(){
        return "/home";
    }

    /** @Description:退出
     * @Author:wrb
     * @Date:2020/1/9/14:58
     * @Param 
     * @return java.lang.String
     */
    @RequestMapping("/loginOut")
    private String loginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return "/login";
    }
}
